﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExaminationSys.DataAccess;
using ExaminationSys.DataAccess.Repository;
using ExaminationSys.Helper;
using Ext.Net;

namespace ExaminationSys.View.SystemManage
{
    public partial class StudentInfoEdit : System.Web.UI.UserControl
    {
        private static readonly StudentService Proxy = ObjectFactory<StudentService>.Create();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                Bind();
            }
        }
        private void Bind()
        {
            // TO DO
        }

        public Store GridStore { get; set; }
        public string ID { get; set; }


        public void SetData(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                UserID.Text = "";
                LoginID.Text = "";
                UserName.Text = "";
                PWD.Text = "";
                CollegeName.Text = "";
                MajorName.Text = "";
                ClassName.Text = "";
                StudentIDHidden.Value = "";
                LockHidden.Value = "0";
                PWDHidden.Value = "123456";
                X.Js.Call("setID", "");
                X.Js.Call("setLock", "0");
                X.Js.Call("setPWD", "123456");
            }
            else
            {
                var realId = new Guid(id);
                var entity = Proxy.Get(realId);

                UserID.Text = entity.UserID;
                LoginID.Text = entity.LoginID;
                UserName.Text = entity.UserName;
                PWD.Text = string.IsNullOrEmpty(entity.PWD) ? "" : DESHelper.Encrypt(entity.PWD);
                CollegeName.Text = entity.CollegeName;
                MajorName.Text = entity.MajorName;
                ClassName.Text = entity.ClassName;
                LockHidden.Value = entity.Lock.ToString();

                ID = id;
                X.Js.Call("setID", id);
                X.Js.Call("setLock", entity.Lock ? 1 : 0);
                X.Js.Call("setPWD", entity.PWD);
            }
        }

        public void Show()
        {
            this.StudentInfoEditWindow.Show();
        }

        protected void SaveStudentInfo(object sender, DirectEventArgs e)
        {
            var userid = UserID.Text.Trim();
            var loginid = LoginID.Text.Trim();
            var pwd = PWD.Text.Trim();
            var username = UserName.Text.Trim();
            var collagename = CollegeName.Text.Trim();
            var majoirname = MajorName.Text.Trim();
            var classname = ClassName.Text.Trim();

            if (string.IsNullOrWhiteSpace(userid))
            {
                MessageBoxHelper.ShowWarning("学号不能为空");
                return;
            }
            if (string.IsNullOrWhiteSpace(loginid))
            {
                MessageBoxHelper.ShowWarning("登陆名不能为空");
                return;
            }
            if (string.IsNullOrWhiteSpace(pwd))
            {
                MessageBoxHelper.ShowWarning("密码不能为空");
                return;
            }
            if (string.IsNullOrEmpty(username))
            {
                MessageBoxHelper.ShowWarning("学生姓名不能为空");
                return;
            }
            //if (string.IsNullOrEmpty(collagename))
            //{
            //    MessageBoxHelper.ShowWarning("学院不能为空");
            //    return;
            //}

            var id = StudentIDHidden.Value;
            var actionflag = string.IsNullOrWhiteSpace(id);
            Guid realId = string.IsNullOrEmpty(id) ? Guid.NewGuid() : new Guid(id);
            if (Proxy.CheckLoginID(loginid, realId))
            {
                MessageBoxHelper.ShowWarning(string.Format("登录名 {0} 已经存在", loginid));
                return;
            }
            if (Proxy.CheckUserID(userid, realId))
            {
                MessageBoxHelper.ShowWarning(string.Format("学号 {0} 已经存在", userid));
                return;
            }
            var op = FormsAuthenticationHelper.GetCurrentUserID();
            ExaminationSys.DataAccess.Entity.Student entity = new DataAccess.Entity.Student();
            entity.ID = realId;
            entity.UserID = userid;
            entity.LoginID = loginid;
            entity.UserName = username;
            //entity.PWD = actionflag ? DESHelper.Encrypt(userid) : PWDHidden.Value;
            entity.PWD = string.IsNullOrEmpty(pwd) ? "" : DESHelper.Encrypt(pwd);
            entity.CollegeName = collagename;
            entity.MajorName = majoirname;
            entity.ClassName = classname;
            entity.Status = 1;
            entity.Lock = LockHidden.Value.Equals("1");
            entity.Operator = string.IsNullOrWhiteSpace(op) ? "n/a" : op;
            entity.OperatorTime = DateTime.Now;
            var sucess = false;

            if (actionflag)
            {
                var retCode = Proxy.Insert(entity);
                sucess = retCode != null;
            }
            else
            {
                sucess = Proxy.Update(entity);
            }

            if (sucess)
            {
                //MessageBoxHelper.ShowInfo("保存成功");
                GridStore.Reload();
                StudentInfoEditWindow.Hide();
            }
            else
            {
                MessageBoxHelper.ShowInfo("保存失败");
                return;

            }
        }
    }
}